﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SaunaClient.Controller.Communication;
using SaunaClient.Model;
using SaunaClient.View;
using SaunaClient.Model.Network;

namespace Admin_Client
{
    public partial class MainForm : Form
    {
        private ICommunicationManagerFactory communicationManagerFactory;
        private ICommunicationManager communicationManager;
        private const string COMMUNICATION_MANAGER = "CommunicationManager";
        private string serverDirectory;

        public MainForm()
        {
            InitializeComponent();
            User user = new User();
            List<string> userList = new List<string>();
            userList = user.ListUsers();
            // Make user authentication controller
            communicationManagerFactory = new CommunicationManagerFactoryImplementation();
            communicationManager = communicationManagerFactory.Make(COMMUNICATION_MANAGER);

            // Initialize controls
            serverDirectory = communicationManager.GetFtpServerDirectory();
            //    userView.

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'saunadDataSet.users' table. You can move, or remove it, as needed.
            this.usersTableAdapter.Fill(this.saunadDataSet.users);

        }

        private void DeleteButton_Click(object sender, EventArgs e)
        {

            AdminClientControler oAdmin = new AdminClientControler();

            oAdmin.ShowForm(typeof(DeleteUser), getSelectedUser());
        }

        private void AddUser_Click(object sender, EventArgs e)
        {
            AdminClientControler oAdmin = new AdminClientControler();

            oAdmin.ShowForm(typeof(NewUserForm));

        }

        private void button6_Click(object sender, EventArgs e)
        {
            AdminClientControler oAdmin = new AdminClientControler();

            oAdmin.ShowForm(typeof(EditUser), getSelectedUser());
        }

        private string[] getSelectedUser()
        {
            string[] arraySelectedUser = new string[8];
            foreach (DataGridViewRow selectedRow in dataGridViewUserList.SelectedRows)
            {
                int i=0;
                foreach (DataGridViewCell selectedCell in selectedRow.Cells)
                {
                    arraySelectedUser[i] = selectedCell.Value.ToString();
                    i++;
                }
            }
            return arraySelectedUser;
        }

        private void analyseStatisicsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AnalyseForm anlayseForm = new AnalyseForm();
            anlayseForm.ShowDialog();
        }

        private void toolsToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void userView_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'saunadDataSet.users' table. You can move, or remove it, as needed.
            this.usersTableAdapter.Fill(this.saunadDataSet.users);
            //dataGridView1.Update();

        }

        private void button5_Click(object sender, EventArgs e)
        {
            // Visual basic style　
            //Initialise list view 
            lstServerFiles.Clear();
            // Create column of list view
            // 120 is the size. Path is Column Name.
            lstServerFiles.Columns.Add("File Name", 120);
            // Size is 110. Column name is Size
            lstServerFiles.Columns.Add("Size", 110);
            // Date Time for column time is 130
            lstServerFiles.Columns.Add("Date Time", 130);
            // Allow for column Allow option is 61
            lstServerFiles.Columns.Add("Allow", 55);


            // Set  directory
            string strDirPath = communicationManager.GetServerFileList(serverDirectory);
            // Gets the list of the files on the local directory.
            string[] arraySelectedFiles = strDirPath.Split(new Char[] { '\n' });
            string fileName;
            string fileRealName;
            foreach (string oneFile in arraySelectedFiles)
            {
                //try catch for empty fields.
                try
                {
                    fileName = oneFile.Substring(39);
                    fileRealName = fileName.TrimEnd(new Char[] { '\r' });
                    // Creates a new listview item for each name
                    ListViewItem listViewItem = new ListViewItem(fileRealName);

                    // Gets the file detail information
                    string date = oneFile.Substring(18, 21);
                    listViewItem.SubItems.Add(date);
                    string size = oneFile.Substring(0, 17);
                    size.Trim();
                    listViewItem.SubItems.Add(size);

                    //listViewItem.SubItems.Add(fileRealName);
                    lstServerFiles.Items.Add(listViewItem);
                }
                catch
                {

                }
            }
        }

        private void filesTxtBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void optionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NetworkSetting networkSetting = communicationManager.ReadConfigFile();
            if (networkSetting == null)
            {
                // When Sauna configuration file does not exist, create network setting with default values
                networkSetting = new NetworkSetting();
            }

            ClientOptionForm clientOptionForm = new ClientOptionForm(networkSetting);

            if (clientOptionForm.ShowDialog() == DialogResult.OK)
            {
                // End Previous Communication
                communicationManager = null;

                // Recreate Communication Manager
                communicationManager = new CommunicationManager();

                networkSetting.Copy(clientOptionForm.networkSetting);
                communicationManager = communicationManagerFactory.Make(COMMUNICATION_MANAGER);
                serverDirectory = networkSetting.FtpServerDirectory;
            }
        }

        private void lstServerFiles_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}